<?php

namespace Home\DAO;

use Home\Common\FIdConst;

/**
 * 习题标签 DAO
 *
 * @author 李静波
 */
class DrillTagDAO extends PSIBaseExDAO {

	/**
	 * 返回习题标签列表
	 *
	 * @param array $params        	
	 * @return array
	 */
	public function tagList($params) {
		$loginUserId = $params["loginUserId"];
		$start = $params["start"];
		$limit = $params["limit"];
		
		$db = $this->db;
		
		$sql = "select t.id, t.name, t.note, t.show_order
				from t_drill_tag t ";
		$queryParams = [];
		$ds = new DataOrgDAO($db);
		$rs = $ds->buildSQL(FIdConst::DRILL_TAG, "t", $loginUserId);
		if ($rs) {
			$sql .= " where " . $rs[0];
			$queryParams = $rs[1];
		}
		
		$sql .= " order by t.show_order
				limit %d, %d ";
		$queryParams[] = $start;
		$queryParams[] = $limit;
		
		$data = $db->query($sql, $queryParams);
		$result = [];
		
		foreach ( $data as $v ) {
			$result[] = [
					"id" => $v["id"],
					"name" => $v["name"],
					"note" => $v["note"],
					"showOrder" => $v["show_order"]
			];
		}
		
		$sql = "select count(*) as cnt
				from t_drill_tag t";
		$queryParams = [];
		$ds = new DataOrgDAO($db);
		$rs = $ds->buildSQL(FIdConst::DRILL_TAG, "t", $loginUserId);
		if ($rs) {
			$sql .= " where " . $rs[0];
			$queryParams = $rs[1];
		}
		
		$data = $db->query($sql, $queryParams);
		$cnt = $data[0]["cnt"];
		
		return [
				"dataList" => $result,
				"totalCount" => $cnt
		];
	}

	/**
	 * 检查参数是否正确
	 *
	 * @param array $params        	
	 * @return array|null
	 */
	private function checkParams(& $params) {
		$name = trim($params["name"]);
		$note = trim($params["note"]);
		
		if ($this->isEmptyStringAfterTrim($name)) {
			return $this->bad("标签名称不能为空");
		}
		
		if ($this->stringBeyondLimit($name, 20)) {
			return $this->bad("标签名称不能超过20个字符");
		}
		
		if ($this->stringBeyondLimit($note, 200)) {
			return $this->bad("备注不能超过200个字符");
		}
		
		return null;
	}

	/**
	 * 新增习题标签
	 *
	 * @param array $params        	
	 * @return array | null
	 */
	public function addDrillTag(& $params) {
		$rc = $this->checkParams($params);
		if ($rc) {
			return $rc;
		}
		
		$name = trim($params["name"]);
		$note = trim($params["note"]);
		$showOrder = intval($params["showOrder"]);
		if (! $showOrder) {
			$showOrder = 0;
		}
		$dataOrg = $params["dataOrg"];
		if ($this->dataOrgNotExists($dataOrg)) {
			return $this->badParam("dataOrg");
		}
		
		$db = $this->db;
		
		// 检查标签名称是否重复
		$sql = "select count(*) as cnt 
				from t_drill_tag 
				where name = '%s' ";
		$data = $db->query($sql, $name);
		$cnt = $data[0]["cnt"];
		if ($cnt > 0) {
			return $this->bad("习题标签[$name]已经存在");
		}
		
		$id = $this->newId();
		
		$sql = "insert into t_drill_tag (id, name, note, show_order, data_org)
				values ('%s', '%s', '%s', %d, '%s')";
		$rc = $db->execute($sql, $id, $name, $note, $showOrder, $dataOrg);
		if ($rc === false) {
			return $this->sqlError(__METHOD__, __LINE__);
		}
		
		$params["id"] = $id;
		
		// 操作成功
		return null;
	}

	/**
	 * 编辑习题标签
	 *
	 * @param array $params        	
	 * @return array | null
	 */
	public function updateDrillTag(& $params) {
		$rc = $this->checkParams($params);
		if ($rc) {
			return $rc;
		}
		
		$db = $this->db;
		
		$id = $params["id"];
		
		$tag = $this->getDrillTagById($id);
		if (! $tag) {
			return $this->bad("要编辑的习题标签不存在");
		}
		
		$name = trim($params["name"]);
		$note = trim($params["note"]);
		$showOrder = intval($params["showOrder"]);
		if (! $showOrder) {
			$showOrder = 0;
		}
		
		// 检查标签名称是否重复
		$sql = "select count(*) as cnt
				from t_drill_tag
				where name = '%s' and id <> '%s' ";
		$data = $db->query($sql, $name, $id);
		$cnt = $data[0]["cnt"];
		if ($cnt > 0) {
			return $this->bad("习题标签[$name]已经存在");
		}
		
		$sql = "update t_drill_tag
				set name = '%s', note = '%s', show_order = %d
				where id = '%s' ";
		$rc = $db->execute($sql, $name, $note, $showOrder, $id);
		if ($rc === false) {
			return $this->sqlError(__METHOD__, __LINE__);
		}
		
		// 操作成功
		return null;
	}

	/**
	 * 根据id查询习题标签
	 *
	 * @param string $id
	 *        	习题标签id
	 * @return array|NULL
	 */
	public function getDrillTagById(string $id) {
		$db = $this->db;
		
		$sql = "select name, note, show_order 
				from t_drill_tag
				where id = '%s' ";
		$data = $db->query($sql, $id);
		if ($data) {
			return [
					"name" => $data[0]["name"],
					"note" => $data[0]["note"],
					"showOrder" => $data[0]["show_order"]
			];
		} else {
			return null;
		}
	}

	/**
	 * 获得某个习题标签的详细信息
	 *
	 * @param string $id
	 *        	习题标签id
	 * @return array
	 */
	public function tagInfo(string $id) {
		$tag = $this->getDrillTagById($id);
		if ($tag) {
			return $tag;
		} else {
			return [];
		}
	}
}